<?php

namespace app\api\service;

use think\facade\Db;

class IntegralService
{
    /**
     * 获取积分赠送比例
     * */
    public static function getGiveIntegralScale($store_id)
    {
        $integralScale = Db::name('integral_setting')->where('store_id',$store_id)->value('give_integral_scale');

        if (empty($integralScale)){
            return  0;
        }

        return $integralScale;
    }

    /**
     * 扣除用户积分
     * */
    public static function deductUserIntegral($user_id,$integral)
    {
        $res = Db::name('user')->where('user_id',$user_id)->dec('integral',$integral)->update();

        return $res;

    }

    /**
     * 获取用户积分
     * */
    public static function getUserIntegral($user_id)
    {
        $integral = Db::name('user')->where('user_id',$user_id)->value('integral');

        return $integral;
    }



    /**
     * 获取积分兑换金豆比例
     * */
    public static function getGoldRedeemScale($store_id)
    {
        $goldRedeemScale = Db::name('integral_setting')->where('store_id',$store_id)->value('integral_gold_scale');

        return $goldRedeemScale;
    }


    /**
     * 积分变动记录
     * */
    public static function saveIntegralChangeRecord($data)
    {
        Db::name('user_integral_detail')->insert([
            'user_id' => $data['user_id'],
            'change_desc' => $data['change_desc'],
            'change_type' => $data['change_type'],
            'change_integral' => $data['change_integral'],
            'change_time' => time()
        ]);
    }
}